package com.ych.modules.api.authorInfc.jackpot.controller;

import com.alibaba.fastjson.JSONObject;
import com.ych.modules.api.authorInfc.jackpot.entity.ApplyJackpotEntity;
import com.ych.modules.api.authorInfc.jackpot.service.JackpotService;
import com.ych.modules.base.dbEntity.TRaffleTicketJackpotEntity;
import com.ych.modules.base.dbEntity.TUserRaffleTicketEntity;
import com.ych.utils.serviceReturn.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created with IntelliJ IDEA.
 * Author: Usopp.tsui
 * Date: 2021/3/15
 * Time: 16:56
 * Description:
 */
@RestController("apiJackpotController")
@RequestMapping("/api/auth/jackpot")
@Api(tags = {"API——抽奖奖池相关"})
public class JackpotController {
    @Autowired
    private JackpotService jackpotService;

    @PostMapping("/addJackpot")
    @ApiOperation(value = "新增奖池", notes = "新增奖池")
    public R<String> addJackpot(@RequestBody TRaffleTicketJackpotEntity entity) {
        try {
            jackpotService.addJackpot(entity);
            return new R(0, "成功", null);
        } catch (Exception e) {
            return new R(1, e.getMessage(), null);
        }
    }

    @PostMapping("/applyJackpot")
    @ApiOperation(value = "使用奖券参加奖池抽奖", notes = "使用奖券参加奖池抽奖")
    public R<String> applyJackpot(@RequestBody ApplyJackpotEntity entity) {
        try {
            if (StringUtils.isBlank(entity.getJackpotId())
                    || StringUtils.isBlank(entity.getRedeemCode())) {
                throw new RuntimeException("必传参数为空");
            }
            TUserRaffleTicketEntity ticketEntity = JSONObject.parseObject(JSONObject.toJSONBytes(entity), TUserRaffleTicketEntity.class);
            jackpotService.applyJackpot(ticketEntity);
            return new R(0, "成功", null);
        } catch (Exception e) {
            return new R(1, e.getMessage(), null);
        }
    }

    @PostMapping("/getJackpot")
    @ApiOperation(value = "获取奖池信息", notes = "获取奖池信息")
    public R<TRaffleTicketJackpotEntity> getJackpot(@RequestBody String jackpotId) {
        try {
            TRaffleTicketJackpotEntity entity = jackpotService.getJackpot(jackpotId);
            return new R(0, "成功", entity);
        } catch (Exception e) {
            return new R(1, e.getMessage(), null);
        }
    }

    @PostMapping("/getCurrentJackpot")
    @ApiOperation(value = "获取当前正在活动的奖池信息", notes = "获取当前正在活动的奖池信息")
    public R<TRaffleTicketJackpotEntity> getCurrentJackpot() {
        try {
            TRaffleTicketJackpotEntity entity = jackpotService.getCurrentJackpot();
            return new R(0, "成功", entity);
        } catch (Exception e) {
            return new R(1, e.getMessage(), null);
        }
    }

    @PostMapping("/runLottery")
    @ApiOperation(value = "奖池颁奖", notes = "奖池颁奖")
    public R<String> runLottery(@RequestBody String jackpotId){
        try {
            jackpotService.runLottery(jackpotId);
            return new R(0,"成功",null);
        }catch (Exception e){
            return new R(1,e.getMessage(),null);
        }
    }
}
